#pragma once
#include<assert.h>
#include"reverse_iterator.h"
namespace zcg
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* prev;
		ListNode<T>* next;
		T _val;
		ListNode(const T& val = T())
			:prev(nullptr)
			,next(nullptr)
			,_val(val)
		{}
	};
	template<class T,class Ref ,class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;
		Node* _node;
		ListIterator(Node* node)
			:_node(node)
		{}
		Self& operator++()
		{
			_node = _node->next;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(_node);
			_node = _node->next;
			return tmp;
		}
		Self& operator--()
		{
			_node = _node->prev;
			return *this;
		}
		Self operator--(int)
		{
			Self tmp(_node);
			_node = _node->prev;
			return tmp;
		}
		Ref operator*()
		{
			return _node->_val;
		}
		Ptr operator->()
		{
			return &(_node->_val);
		}
		bool operator!=(const Self& s)const
		{
			return _node != s._node;
		}
		bool operator ==(const Self& s)
		{
			return _node == s._node;
		}		
	};
	template<class T>
	class List
	{
		typedef ListNode<T> Node;
	public:
		typedef  ListIterator<T, T&, T*> iterator;
		typedef  ListIterator<T,const T&,const T*> const_iterator;
		typedef  Reverseiterator<iterator, T&, T*> reverse_iterator;
		typedef Reverseiterator<const_iterator, const T&, const T*> const_reverse_iterator;
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_reverse_iterator rbegin()const
		{
			return const_reverse_iterator(end());
		}
		const_reverse_iterator rend()const
		{
			return const_reverse_iterator(begin());
		}

		iterator begin()
		{
			return _head->next;
		}
		const_iterator begin()const
		{
			return _head->next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator end()const 
		{
			return _head;
		}
		void empty_init()
		{
			_head = new Node;
			_head->prev = _head;
			_head->next = _head;
			_size = 0;
		}
		List()
		{
			empty_init();
		}
		void swap(List<T>& tmp)
		{
			std::swap(tmp->_head, _head);
			std::swap(tmp->_size, _size);
		}

		List(List<T>& tmp)
		{
			swap(tmp);
		}
	    void clear()
		{
			iterator it = begin();
			while(it != end())
			{
				it=erase(it);
			}
		}
		void insert(iterator pos,const T&x)
		{
			Node* cur = pos._node;
			Node* pre = cur->prev;
			Node* newnode = new Node(x);
			pre->next = newnode;
			newnode->next = cur;
			newnode->prev = pre;
			cur->prev = newnode;
			_size++;
			//return newnode;
		}
		iterator erase(iterator pos)
	    {
			assert(pos != end());
			Node* cur = pos._node;
			Node* pre = cur->prev;
			Node* next = cur->next;
			pre->next = next;
			next->prev = pre;
			delete cur;
			cur = nullptr;
			_size--;
			return next;
		}
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		void pop_back()
		{
			erase(--end());
		}
		void push_front(const T&x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			erase(begin());
		}
		size_t size()
		{
			return _size;
		}
		~List()
		{
			clear();
			delete _head;
			_head = nullptr;
		_size = 0;
		}
	private:
		Node*_head;
		size_t _size;
	};
}
